package touhou.entity;

import gameframework.base.Drawable;
import gameframework.game.SpriteManager;
import gameframework.game.SpriteManagerDefaultImpl;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;

import touhou.base.Entity;
import touhou.base.Overlappable;
import touhou.base.Shooter;
import touhou.base.ShooterClient;
import touhou.game.GameMovable;

/**
 * GameEntity Jouable.
 * C'est le Heros du jeu !
 */
public class Hero extends GameMovable implements Drawable, Entity,
		Overlappable, ShooterClient {
	protected final SpriteManager spriteManager;
	public static final int RENDERING_SIZE = 16;
	protected boolean movable = true;
	private int hp_left;
	private int COOLDOWN = 4;
	private int cooldown = 0;
	private Entity shooter;
	
	private Canvas c;

	public Hero(Canvas canvas) {
		spriteManager = new SpriteManagerDefaultImpl("images/pac1.gif",
				canvas, RENDERING_SIZE, 6);
		spriteManager.setTypes(
				//
				"right", "left", "up",
				"down",//
				"invulnerable-right", "invulnerable-left", "invulnerable-up",
				"invulnerable-down", //
				"unused", "static", "unused");
		hp_left = 1;
		
		c = canvas;
	}

	@Override
	public Rectangle getBoundingBox() {
		int a = RENDERING_SIZE/2;
		return (new Rectangle((int)getPosition().getX()+a, (int)getPosition().getY()+a, 1, 1));
	}

	@Override
	public void draw(Graphics g) {
		String spriteType = "";
		Point2D tmp = getSpeedVector().getDirection();
		movable = true;

		if (tmp.getX() == 1) {
			spriteType += "right";
		} else if (tmp.getX() == -1) {
			spriteType += "left";
		} else if (tmp.getY() == 1) {
			spriteType += "down";
		} else if (tmp.getY() == -1) {
			spriteType += "up";
		} else {
			spriteType = "static";
			spriteManager.reset();
			movable = false;
		}
		spriteManager.setType(spriteType);
		Point2D p = getPosition();
		spriteManager.draw(g, new Point((int)p.getX(),(int)p.getY()));

		int a = RENDERING_SIZE/2;
		g.drawRect((int)getPosition().getX()+a-2, (int)getPosition().getY()+a-2, 3, 3);
	}

	@Override
	public void oneStepMoveAddedBehavior() {
		if (movable) {
			spriteManager.increment();
		}
		cooldown--;
	}
	
	public void accept(Shooter v) {
		shooter = v;
		v.visit(this);
	}

	public void damage(int damage) {
		hp_left -= damage;
	}
	
	public boolean isDead() {
		return hp_left<=0;
	}

	@Override
	public boolean canShoot() {
		if (cooldown <= 0) {
			cooldown += COOLDOWN;
			return true;
		}
		return false;
	}

	@Override
	public Entity shooter() {
		return shooter;
	}
}
